package org.lanqiao.dao;

import org.lanqiao.util.SqlUtil;
import org.lanqiao.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.lanqiao.pojo.OrderListPOJO;
import org.lanqiao.pojo.PageModel;
import org.lanqiao.pojo.ProductPOJO;

/**
 * 订单管理
 * @author 孙鑫凯
 *
 */
public class OrderDaoImpl {
	/**
	 * 插入订单管理
	 */
	public int orders(OrderListPOJO n,String Id){
		int a=0;
		try {
			String sql2="Insert into order_list(ORDER_ID,ORDER_USER_ID,ORDER_TOTAL_PRICE,ORDER_COMMENT,ORDER_STATUS,ORDER_DATE_TIME,ORDER_DSTATUS)values(?,?,?,?,1,NOW(),1)";	
			a=SqlUtil.update(sql2,Id,n.getUserId(),n.getTotalPrice(),n.getComment());
			return a;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
		}
	/**
	 * 更新购物车
	 */
	public int updateOrder(String orderId,List list) {
		int a=0;
		for(int i=0;i<list.size();i++) {
			try {
				String sql="UPDATE car_list set CAR_ORDER_ID = ?,CAR_STATUS='0' WHERE CAR_ID =?";
				System.out.println(orderId+"|||"+list.toString());
				a+=SqlUtil.update(sql,orderId,list.get(i));
			} catch (Exception e) {
				throw new RuntimeException(e);
			}	
		}
		return a;
	}
	/**
	 * 删除订单管理
	 */
	public int deleteOrders(String id) {
		OrderListPOJO order=new OrderListPOJO();
		int a=0;
		try {
			String sql="UPDATE order_list set ORDER_DSTATUS=0 where Order_Id=?";	
			a=SqlUtil.update(sql,id);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return a;
	}
	/**
	 * 修改订单
	 */
	public int amendOrder(String price,String id) {
		int a=0;
		try {
			String sql="UPDATE order_list set ORDER_TOTAL_PRICE = ? WHERE Order_Id =?";	
			a=SqlUtil.update(sql,price,id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
		return a;
	}
	/**
	 * 查询订单
	 */
	public List queryOrder(String id) {
		OrderListPOJO des=null;
		List str1=new ArrayList();
		String sql="select * from order_list  where ORDER_ID=? and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		try {
			List<HashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){
				for(HashMap<String,Object> map :maps) {
					des =new OrderListPOJO();
					des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
					des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
					des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
					des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
					des.setDatetime(map.get("ORDER_DATE_TIME")==null?null:map.get("ORDER_DATE_TIME").toString());
				str1.add(des);
				}
				}
			
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str1;
	}
	/**
	 * 查询已付款订单
	 */
	public List alreadyPaid(String id) {
		OrderListPOJO des=null;
		List str1=new ArrayList();
		String sql="select * from order_list where ORDER_USER_ID=? and ORDER_IS_PAY=1 and ORDER_IS_SEND=0 and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		try {
			List<HashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){
				for(HashMap<String,Object> map :maps) {
					des =new OrderListPOJO();
					des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
					des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
					des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
					des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
					des.setDatetime(map.get("ORDER_DATE_TIME")==null?null:map.get("ORDER_DATE_TIME").toString());
				str1.add(des);
				}
				}
			
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str1;
	}
	/**
	 * 查询已发货订单
	 */
	public List<OrderListPOJO> goodsReceived(String id) {
		OrderListPOJO des=null;
		List<OrderListPOJO> str1=new ArrayList<OrderListPOJO>();
		String sql="select * from order_list where ORDER_USER_ID=? and ORDER_IS_SEND=1 and ORDER_IS_ARRIVE=0 and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		try {
			List<HashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){
				for(HashMap<String,Object> map :maps) {
					des =new OrderListPOJO();
					des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
					des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
					des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
					des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
					des.setDatetime(map.get("ORDER_DATE_TIME")==null?null:map.get("ORDER_DATE_TIME").toString());
				str1.add(des);
				}
				}
			
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str1;
	}
	
	/**
	 * 查询已收货订单
	 */
	public List<OrderListPOJO> receivedGoods(String id) {
		OrderListPOJO des=null;
		List<OrderListPOJO> str1=new ArrayList<OrderListPOJO>();
		String sql="select * from order_list where ORDER_USER_ID=? and ORDER_IS_ARRIVE=1 and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		try {
			List<LinkedHashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){
				for(HashMap<String,Object> map :maps) {
					des =new OrderListPOJO();
					des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
					des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
					des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
					des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
					des.setDatetime(map.get("ORDER_DATE_TIME")==null?null:map.get("ORDER_DATE_TIME").toString());
					str1.add(des);
				}
				}
			
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str1;
	}
	/**
	 * 查询未付款订单
	 */
	public List Unpaid(String id) {
		
		List str1=new ArrayList();
		String sql="select * from order_list where ORDER_USER_ID=? and ORDER_STATUS=1 and ORDER_IS_PAY=0 and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		try {
			List<HashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){
				for(HashMap<String,Object> map :maps) {
					OrderListPOJO des =new OrderListPOJO();
					des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
					des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
					des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
					des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
					des.setDatetime(map.get("ORDER_DATE_TIME")==null?null:map.get("ORDER_DATE_TIME").toString());
				str1.add(des);
				}
				}
			
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str1;
	}
	
	/**
	 * 订单结算
	 */
	public int orderSettlement(String id) {
		int a=0;
		try {
			String sql="UPDATE order_list set ORDER_IS_PAY = 1 WHERE Order_Id =?";
			a=SqlUtil.update(sql,id);
			} catch (Exception e) {
			throw new RuntimeException(e);
	}	
	 return a;
	}
	/**
	 * 取消订单
	 */
	public int cancellationOrder(String id) {	
		int a=0;
		try {
			String sql="UPDATE order_list set ORDER_STATUS =0 WHERE ORDER_ID=?";
			a=SqlUtil.update(sql,id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}	
		return a;
	}
	
	/**
	 * 订单详情
	 */
	public List  orderDetails(String id) {
		OrderListPOJO des=null;
		List str=new ArrayList();
		String sql1="select * from order_list where ORDER_ID=? and ORDER_DSTATUS=1";
		List<HashMap<String,Object>> maps1=SqlUtil.select21(sql1,id);
		if(maps1!=null && maps1.size()>0) {
			for(HashMap<String,Object> map :maps1) {
				des =new OrderListPOJO();
				des.setId(map.get("ORDER_ID")==null?null:map.get("ORDER_ID").toString());
				des.setUserId(map.get("ORDER_USER_ID")==null?null:map.get("ORDER_USER_ID").toString());
				des.setComment(map.get("ORDER_COMMENT")==null?null:map.get("ORDER_COMMENT").toString());
				des.setTotalPrice(map.get("ORDER_TOTAL_PRICE")==null?null:map.get("ORDER_TOTAL_PRICE").toString());
			}
		}
		List str1=new ArrayList();
		String sql="select * from order_list O left join car_list C on O.ORDER_ID=C.CAR_ORDER_ID left join product P on C.CAR_PRODUCT_ID=P.PRODUCT_ID where O.ORDER_ID=? and ORDER_DSTATUS=1";
		try {
			List<HashMap<String,Object>> maps=SqlUtil.select21(sql,id);
			if(maps!=null && maps.size()>0){	
				for(HashMap<String,Object> map :maps) {
					ProductPOJO pro=new ProductPOJO();
					pro.setProductName(map.get("PRODUCT_NAME")==null?null:map.get("PRODUCT_NAME").toString());
					pro.setPhoto(map.get("PRODUCT_PHOTO")==null?null:map.get("PRODUCT_PHOTO").toString());
					pro.setPrice(map.get("PRODUCT_PRICE")==null?null:map.get("PRODUCT_PRICE").toString());
					pro.setStandard(map.get("PRODUCT_STANDARD")==null?null:map.get("PRODUCT_STANDARD").toString());
					pro.setUnit(map.get("CAR_PRODUCT_COUNT")==null?null:map.get("CAR_PRODUCT_COUNT").toString());
					pro.setProductColor(map.get("PRODUCT_COLOR")==null?null:map.get("PRODUCT_COLOR").toString());
				str1.add(pro);
				}
				}
			des.setList(str1);
			str.add(des);
		} catch (Exception e) {
		  throw new RuntimeException(e);	 
		}
		return str;
	}
	
	/**
	 * 分页查询订单
	 */
	public PageModel<List> listOrder(String userId,String page,String pageSize){
		String sql="select * from order_list where ORDER_USER_ID=? and ORDER_DSTATUS=1 ORDER BY ORDER_DATE_TIME DESC";
		PageModel pageModel=new PageModel<>(sql,page,pageSize);
		OrderListPOJO des=null;
		List<HashMap<String,Object>> list=SqlUtil.select21(pageModel.toMysqlSql(),userId);
		List<OrderListPOJO> list2=new ArrayList<OrderListPOJO>();
		if(list.size()>0) {
			for(HashMap<String,Object> map: list) {
				des =new OrderListPOJO();
				des.setId(StringUtil.valueOf(map.get("ORDER_ID")));
				des.setUserId(StringUtil.valueOf(map.get("ORDER_USER_ID")));
				des.setComment(StringUtil.valueOf(map.get("ORDER_COMMENT")));
				des.setTotalPrice(StringUtil.valueOf(map.get("ORDER_TOTAL_PRICE")));
				des.setDatetime(StringUtil.valueOf(map.get("ORDER_DATE_TIME")));
			list2.add(des);
			}
		}
//		把list转换为对象
		pageModel.setList(list2);
		List<HashMap<String,Object>> countList=SqlUtil.select21(pageModel.toCountSql(),userId);
		String total=StringUtil.empty(countList.get(0).get("count"));
		pageModel.setTotal(Integer.valueOf(total));
		pageModel.setTotalPage(pageModel.getTotalPage());
		return pageModel;
	}
}
